$(document).ready(function() {

    // fix hight
    $('#main').css('height', $('.checkout_1').height());

    $('#add_bonus_equipment').live('click', function() {
        // show popup 
        jQuery('#bpopup_extras').bPopup({},
                function() {

                    //initialzie when popup is visible
                });
        return false;
    })

    $('.remove-button').live('click', function() {
        var id = $(this).attr('data-holder');
        $('#extra-' + id).removeAttr('checked');
        submit_extras($(this));
        return false;
    });

    $('.checkout_1_button').live('click', function() {
        $('.checkout_1').fadeOut();
        $('.checkout_2').fadeIn(700);
        $('#aside ol li:first-child').removeClass('active');
        $('#aside ol li:eq(1)').addClass('active');
        $('#main').css('height', $('.checkout_2').height());
        return false;
    })

    $('.checkout_2_back').live('click', function() {
        $('.checkout_2').fadeOut(100, function() {
            $('#main').css('height', $('.checkout_1').height());
        });
        $('.checkout_1').fadeIn(300);
        $('#aside ol li:eq(1)').removeClass('active');
        $('#aside ol li:first-child').addClass('active');
        return false;
    })


    $('#terms_buttom').live('click', function() {
        //        // show popup 
        //        jQuery('#bpopup_terms').bPopup({}, 
        //            function() {
        //
        //            //initialzie when popup is visible
        //            });
        //        return false;

        show_page(114);
    })
    $('#close_terms').live('click', function() {
        $('.bClose').trigger('click');
    })

    //$('#add_bonus_equipment').trigger('click');
    //$('.checkout_1_button').trigger('click');
    //$('#terms_buttom').trigger('click');

    /*
     * Subit customer form
     */
    $("#customer_form").validate({
        rules: {
            name: "required",
            phone: "required",
            address: "required",
            city: "required",
            zip: "required",
            email: {
                required: true,
                email: true
            }
        },
        submitHandler: function(form) {

            $('#customer_submit_button').busy();

            if ($("#country").val() == 0) {
                alert("Please select Country.\n\nThen re-submit the form.");
                $('#customer_submit_button').busy('hide');
                return false;
            }

            if ($("#terms").is(":checked")) {

                $('#customer_submit_button').attr('disabled', 'disabled');
                var form = $("#customer_form");
                var form_data = form.serialize();

                $.ajax({
                    url: form.attr('action'),
                    dataType: 'json',
                    type: "POST",
                    data: form_data,
                    success: function(data, textStatus, xhr) {

                        if (data.response) {

                            var id;

                            switch (platform_class) {
                                case "rentacar":
                                    id = data.json['id'];
                                    break;
                                case "excursions":
                                    id = data.json['booking_id'];
                                    break;
                                case "hotels":
                                    id = data.json['reservation_id'];
                                    break;
                            }

                            var url_to = base_url + lang + '/' + platform_class + '/checkout_show_order/' + id;
                            location.href = url_to;

                            $('#customer_submit_button').busy('hide');
                        } else {
                            alert(data.msg);
                        }
                        //that.busy("hide");
                    },
                    error: function(xhr, textStatus, errorThrown) {

                        alert('Method on server not found.');
                        $('#customer_submit_button').removeAttr('disabled');

                    }
                });

            } else {

                alert("Please agree with Terms and Conditions'.\n\nThen re-submit the form.");
                $('#customer_submit_button').busy('hide');
            }

        },
        errorPlacement: function(error, element) {
        }
    });

    $('#customer_submit_button').removeAttr('disabled');

});